package ipdesk.model.report.brand.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.ibatis.dao.client.DaoManager;

import ipdesk.model.report.brand.bean.BrandInfoBean;


import ecg.dao.DaoService;


public class BrandInfoDao
{
	private static BrandInfoDao singleton;
		
	protected BrandInfoDao(){}
	
	public static BrandInfoDao getInstance()
	{
		if(singleton == null)
		{
			singleton = new BrandInfoDao();
		}
		
		return singleton;
	}
	
	private DaoManager	manager	= DaoService.getInstance();
	private IBrandInfo brandInfo 	= (IBrandInfo)manager.getDao(IBrandInfo.class, "ip_desk");
	
	/*
	 * 입력받은 데이터를 DB에 insert 하는 메서드.
	 */
	public int brandInfoReg(BrandInfoBean brandInfoBean) throws Exception
	{
		int lastBrandSeq = 0;
		
		try{
			// 트랜잭션 시작
			manager.startTransaction();
			
			
			
			// 상담정보 insert.
			lastBrandSeq = brandInfo.brandInfoReg(brandInfoBean);
		
			// 트랜잭션 커밋.
			manager.commitTransaction();
		}finally{
			// 트랜잭션 끝.
			manager.endTransaction();
		}
					
	
		return lastBrandSeq;
	}
	
	public void insertFileData(BrandInfoBean brandInfoBean) throws Exception
	{
		try{
			manager.startTransaction();
			
			brandInfo.insertFileData(brandInfoBean);
			
			manager.commitTransaction();
		}finally{
			manager.endTransaction();
		}				
		
	}
	
	public int getDataTotalCount(BrandInfoBean brandInfoBean) throws Exception
	{
		System.out.println("년도 : " + brandInfoBean.getYear());
		int totalDataCount = brandInfo.getDataTotalCount(brandInfoBean); 
		
		return totalDataCount;
	}
	
	public List<BrandInfoBean> getPagingDataListForUser(Map map) throws Exception
	{
		
		List<BrandInfoBean> pageDataList = brandInfo.getPagingDataListForUser(map);
		

		return pageDataList; 
	}
	
	public BrandInfoBean getDetailData(int brandSeq) throws Exception
	{
		BrandInfoBean brandInfoBean = brandInfo.getDetailData(brandSeq);
		
		//brandInfoBean.setEmblem(brandInfoBean.getEmblem().replace("<br/>", "\n"));
		//brandInfoBean.setSimilarBrand(brandInfoBean.getSimilarBrand().replace("<br/>", "\n"));
		if(brandInfoBean.getResultMemo() != null) {
			brandInfoBean.setResultMemo(brandInfoBean.getResultMemo().replace("\n", "<br/>"));
		}
		
		if(brandInfoBean.getBrandName() != null) {
			brandInfoBean.setBrandName(brandInfoBean.getBrandName().replace("\n", "<br/>"));
		}
		
		
		return brandInfoBean;
	}
	
	public void insertCompleteData(BrandInfoBean brandInfoBean) throws Exception {
		brandInfo.insertCompleteData(brandInfoBean);
	}
	
	public BrandInfoBean getFileDetailData(int brandSeq, String fileCategory) throws Exception {
		BrandInfoBean brandInfoBean = new BrandInfoBean();
		brandInfoBean.setBrandSeq(brandSeq);
		brandInfoBean.setFile_category(fileCategory);
		
		return brandInfo.getFileDetailData(brandInfoBean);
	}
	
	public void brandInfoUpdate(BrandInfoBean brandInfoBean) throws Exception
	{
		try{
			manager.startTransaction();

			brandInfo.brandInfoUpdate(brandInfoBean);
			
			manager.commitTransaction();
		}finally{
			manager.endTransaction();
		}
	}
	
	public void delFileData(String fileSeq) throws Exception
	{
		brandInfo.delFileData(fileSeq);
	}
	
	public void delDetailData(int brandSeq) throws Exception
	{
		brandInfo.delDetailData(brandSeq);
	}
	
	public void delCompleteData(int brandSeq) throws Exception
	{
		brandInfo.delCompleteData(brandSeq);
	}
}
